package com.kingscup.gamestates;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.kingscup.Storage;

public class PlayerMenu extends BasicGameState {

	Input input;
	
	private int stateID;
	
	static AngelCodeFont font;
	static String[] playerNames;

	int pressed = 0;

	boolean amountone, amounttwo, amountthree, amountfour, amountfive,
			amountsix;

	Image arrows;
	Image playerSheet;
	Image submit;
	MouseOverArea arrowLeft;
	MouseOverArea arrowRight;
	MouseOverArea submitt;

	public PlayerMenu(int stateID) {
		this.stateID = stateID;
	}
	
	
	@Override
	public void init(GameContainer gameContainer, StateBasedGame sbg)
			throws SlickException {
		input = gameContainer.getInput();
		playerNames = new String[5];
		font = new AngelCodeFont("assets/demo2.fnt", "assets/demo2_00.tga");
		arrows = new Image("assets/arrows.png");
		arrowLeft = new MouseOverArea(gameContainer, arrows.getSubImage(0, 0,
				50, 50), 380, 335);
		arrowRight = new MouseOverArea(gameContainer, arrows.getSubImage(50, 0,
				50, 50), 540, 335);
		playerSheet = new Image("assets/playersheet.png");
		submit = new Image("assets/submit.png");
		submitt = new MouseOverArea(gameContainer, submit, 440, 440);
	}

	@Override
	public void render(GameContainer gameContainer, StateBasedGame sbg,
			Graphics g) throws SlickException {
		g.setFont(font);
		g.drawString("Anna pelaajien määrä", 400, 250);
		g.drawString(Integer.toString(Storage.players), 480, 350);

		// Piirtää ruudulle pelaajien ikonit ja tekstikentät nimen kirjoittamiseen

		if (Storage.players == 2) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
		}
		if (Storage.players == 3) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
		}
		if (Storage.players == 4) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
		}
		if (Storage.players == 5) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
			playerSheet.getSubImage(150, 150, 150, 150).draw(450, 450);
		}
		if (Storage.players == 6) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
			playerSheet.getSubImage(150, 150, 150, 150).draw(450, 450);
			playerSheet.getSubImage(300, 150, 150, 150).draw(560, 450);
		}

		arrowLeft.render(gameContainer, g);
		arrowRight.render(gameContainer, g);
		submitt.render(gameContainer, g);
	}

	@Override
	public void update(GameContainer gameContainer, StateBasedGame sbg,
			int delta) throws SlickException {

		input = gameContainer.getInput();
		input.clearMousePressedRecord();

		if (arrowLeft.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players > 2)) {

			pressed++;
			if (pressed == 1) {
				Storage.players -= 1;
			}

		}

		if (arrowRight.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players < 6)) {

			pressed++;
			if (pressed == 1) {
				Storage.players += 1;
			}
		}

		if (!input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
			pressed = 0;
		}

		// Kun valitaan pelaajamäärä ja painetaan OK -nappulaa niin tehdään
		// jotain. Tein pelaajamäärille tyhjät
		// if-touhut valmiiksi. Noi amountjutut on vaan ruudulle tulostamisen
		// testausta varten.
		// Jokaseen noihin sitte tulevaisuudessa jutut että piirtää oikeen
		// määrän pelaajia ruudulle jne
		if (submitt.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players == 2)) {

			sbg.enterState(Storage.PLAYERNAMEMENU);
			Storage.players = 2;
			amounttwo = true;
			amountone = false;
		}
		if (submitt.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players == 3)) {
			sbg.enterState(Storage.PLAYERNAMEMENU);
			amounttwo = true;
			amountone = false;
		}
		if (submitt.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players == 4)) {
			sbg.enterState(Storage.PLAYERNAMEMENU);
			amounttwo = true;
			amountone = false;
		}
		if (submitt.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players == 5)) {
			sbg.enterState(Storage.PLAYERNAMEMENU);
			amounttwo = true;
			amountone = false;
		}
		if (submitt.isMouseOver()
				&& input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
				&& (Storage.players == 6)) {
			sbg.enterState(Storage.PLAYERNAMEMENU);
			amounttwo = true;
			amountone = false;
		}

	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}

}